package fr.chrysalis.ui.components.input
{
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import fr.chrysalis.ui.assets.IAsset;
	import fr.chrysalis.ui.components.events.InputEvent;
	import fr.chrysalis.ui.components.events.InputTextEvent;
	import fr.chrysalis.ui.components.interfaces.IComponent;
	import fr.chrysalis.ui.components.core.Style;

	public class InputText extends Input implements IComponent
	{
		
		/**
		 * Récupère la valeur de l'input
		 */
		override public function get value():String
		{
			return this.input.text;
		}
		/**
		 * Modifie la valeur de l'input
		 * @param value Valeur de l'input
		 */
		override public function set value(value:String):void
		{
			try { this.input.text = value; } catch(e:Error){ }
			super.value = value;
		}
		
		/**
		 * Instance du texte de saisie 
		 */		
		public var input:TextField;
		
		/**
		 * Crée un nouveau bouton 
		 * @param asset Asset à assigner au bouton
		 */
		public function InputText(asset:IAsset)
		{
			super(asset);
			this.input = this.element.txt1.txt;
			this.input.addEventListener(FocusEvent.FOCUS_OUT, handlerFocusOut);
			this.input.addEventListener(FocusEvent.FOCUS_IN, handlerFocusIn);
			this.enable();
			this.focusDisable();
		}
		
		/**
		 * Applique un style au texte
		 * @param value Style
		 */
		public function setStyle(style:Style):void
		{
			this.input.defaultTextFormat = style.format;
			this.input.setTextFormat(style.format);
			this.input.defaultTextFormat = style.format;
		}
		
		/**
		 * Active le focus
		 */
		override public function focusEnable():void {
			this.input.tabEnabled = true;
		}
		
		/**
		 * Désactive le focus
		 */
		override public function focusDisable():void {
			this.input.tabEnabled = false;
		}
		
		/**
		 * Active l'input 
		 */
		override public function enable():void {
			super.enable();
			this.play('enable');
			this.element.area.addEventListener(MouseEvent.ROLL_OVER, handlerMouseRollOver);
			this.element.area.addEventListener(MouseEvent.ROLL_OUT, handlerMouseRollOut);
		}
		
		/**
		 * Désactive l'input 
		 */
		override public function disable():void {
			super.disable();
			this.play('disable');
			this.element.area.removeEventListener(MouseEvent.ROLL_OVER, handlerMouseRollOver);
			this.element.area.removeEventListener(MouseEvent.ROLL_OUT, handlerMouseRollOut);
		}
		
		/**
		 * Simule un roll over sur le bouton 
		 */
		public function rollOver():void{
			if(!this.isFocus){
				this.play('over');
			}
			this.dispatchEvent(new InputTextEvent(InputTextEvent.ROLL_OVER));
		}
		
		/**
		 * Simule un roll out sur le bouton 
		 */
		public function rollOut():void{
			if(!this.isFocus){
				this.play('enable');
			}
			
		}
		
		/**
		 * Simule un press sur le bouton 
		 */
		private function mouseDown():void{
			if(!this.isFocus){
				this.play('focus');
			}
			this.isFocus = true;
			this.focusEnable();
		}
				
		/**
		 * Simule un relachement du click sur le bouton 
		 */
		private function mouseUp():void{
		}
		
		/**
		 * Simule un click sur le bouton 
		 */
		public function focus():void{
			this.mouseDown();
		}
		
		/**
		 * Handler lors d'un MouseEvent.ROLL_OVER 
		 * @param evt
		 */
		private function handlerMouseRollOver(evt:MouseEvent):void {
			this.rollOver();
			this.element.area.addEventListener(MouseEvent.MOUSE_DOWN, handlerMouseDown);
		}
		
		/**
		 * Handler lors d'un MouseEvent.ROLL_OUT  
		 * @param evt
		 */
		private function handlerMouseRollOut(evt:MouseEvent):void {
			this.rollOut();
			this.element.area.removeEventListener(MouseEvent.MOUSE_DOWN, handlerMouseDown);
			this.element.area.removeEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
			this.dispatchEvent(new InputTextEvent(InputTextEvent.ROLL_OUT));
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_UP
		 * @param evt
		 */
		private function handlerMouseUp(evt:MouseEvent):void {
			this.mouseUp();
			this.element.area.removeEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_DOWN
		 * @param evt
		 */
		private function handlerMouseDown(evt:MouseEvent):void {
			this.mouseDown();
			this.container.stage.focus = this.input;
			this.element.area.addEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
		}
		
		/**
		 * Handler lors d'un FocusEvent.FOCUS_IN
		 * @param evt
		 */
		private function handlerFocusIn(evt:FocusEvent):void {
			this.mouseDown();
			this.dispatchEvent(new InputEvent(InputEvent.FOCUS_IN));
		}
		
		/**
		 * Handler lors d'un FocusEvent.FOCUS_OUT
		 * @param evt
		 */
		private function handlerFocusOut(evt:FocusEvent):void {
			this.isFocus = false;
			this.rollOut();
			this.dispatchEvent(new InputEvent(InputEvent.FOCUS_OUT));
		}
		
		override public function toString():String {
			return '[InputText name : ' + this.name + ']';
		}
		
	}
}